//
//  ViewController.m
//  JavaScript
//
//  Created by lizp on 2016/12/10.
//  Copyright © 2016年 Apple05. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //webview用于加载html
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 300)];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    
    //获取html输入框内容
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 -60, 350, 120, 30);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"获取html输入框内容" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    //错误测试
    UIButton *errBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    errBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 -60, CGRectGetMaxY(btn.frame) + 20, 120, 30);
    errBtn.backgroundColor = [UIColor redColor];
    [errBtn setTitle:@"错误测试" forState:UIControlStateNormal];
    errBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [errBtn addTarget:self action:@selector(errorText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:errBtn];
    
    //获取bundle包路径     此处应注意bundle的生成以及bundle文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"xheditor" ofType:@"bundle"];
    //作为html加载文件的顶级路径
    NSURL *baseUrl = [NSURL fileURLWithPath:path];
    
    //加载本地html
    NSString *htmlPath = [[NSBundle bundleWithPath:path] pathForResource:@"xheditor" ofType:@"html"];
    NSString *htmlContext = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlContext baseURL:baseUrl];
    
}

#pragma mark - UIWebViewDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView {

    //设置javaScriptContext的上下文
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //将jsObj对象引向自身   jsObj可自己定义（js对象），在js中调用javaScriptCoreDelegate的代理方法
    self.jsContext[@"jsObj"] = self;
    //JSContext发生错误时捕获异常
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        context.exception = exception;
        NSLog(@"异常:%@",exception);
        
    };
}


#pragma mark - javaScriptCoreDelegate代理   （js调用oc的方法）
-(void)getTextViewContent:(NSString *)content {

    NSLog(@"输入js输出框中的内容:%@",content);
}

-(void)jsCall {

    NSLog(@"js调用oc");
}

#pragma mark - 获取html输入框内容 （oc调用js）
-(void)btnClick {
    
    
    //用oc方法调用js方法    getTextViewContent()为js方法
    NSString *js = [NSString stringWithFormat:@"getTextViewContent()"];
    [self.webView stringByEvaluatingJavaScriptFromString:js];

}

#pragma mark -错误测试    发生错误被JSContext捕获
-(void)errorText {

    [self.jsContext evaluateScript:@"jsError();"];
}



@end
